{{define "cms/user_edit.html"}}
{{ template "header" . }}

<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        权限管理 / 用户管理 / 编辑用户
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="form-horizontal">
                        <input type="hidden" name="token" value="{{ .token }}">
                        <input type="hidden" placeholder="" name="id" value="{{.obj.ID}}" class="form-control">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名：</label>
                            <div class="col-sm-3">
                                <input type="text" placeholder="" name="name" value="{{.obj.Name}}" class="form-control" required> <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">密码：</label>
                            <div class="col-sm-3">
                                <input type="password" placeholder="" name="password" class="form-control"> <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">角色分配：</label>
                            <div class="col-sm-3">
                                <select class="form-control m-b" name="role_id" aria-label="">
                                    <option value="0">--请选择角色--</option>
                                    {{ range .lists }}
                                        <option value="{{ .ID }}" {{if eq .ID $.roleId}}selected{{end}}>{{ .Name }}</option>
                                    {{end}}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-3">
                                <button class="btn btn-sm btn-primary" onclick="edit_user()">修 改</button>
                                <a href="/user" class="btn btn-sm btn-white">取消</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>


{{ template "footer" . }}
<script>
    function edit_user(){
        var token = $("input[name='token']").val();
        var id = $("input[name='id']").val();
        var name = $("input[name='name']").val();
        var passwd = $("input[name='password']").val();
        var role_id = $("select[name='role_id']").val();
        var prompt_msg = "";
        if(name === ""){
            prompt_msg = "用户名不能为空";
        }else if(passwd === ""){
            prompt_msg = "密码不能为空";
        }else if(role_id === "0"){
            prompt_msg = "请选择角色";
        }
        if (prompt_msg !== ""){
            layer.msg(prompt_msg, {icon: 2, time: 1200, title: "提示"});
            return;
        }

        $.post("/useredit", {id:id, name:name, passwd:passwd, token: token, role_id:role_id},function (res) {
            var jsonData = $.parseJSON(res);
            if(jsonData.status !== 200 && jsonData.status !== 20023){
                if(jsonData.status !== 20007){
                    $("input[name='token']").val(jsonData.data);
                }
                layer.msg(jsonData.message, {icon: 2, time: 1200, title: "提示"});
            } else if(jsonData.status === 20023) {
                window.location.href = "/nopermission";
            } else {
                layer.msg(jsonData.message, {
                    icon: 1,
                    time: 1000,
                    title: "提示",
                    end:function () {
                        window.location.href = "/user";
                    }
                });
            }
        });
    }
</script>
{{end}}